//
//  DetailController.m
//  YouMin_iOS
//
//  Created by FanYanLong on 15/12/10.
//  Copyright © 2015年 wey. All rights reserved.
//

#import "DetailController.h"
#import "DetailCell.h"
#import "DetailModel.h"
#import "CQ_FanDataDownloadTools.h"
@interface DetailController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UIView *Dview;
//头像
@property(nonatomic,strong)UIImageView *PicImageView;
//名字
@property(nonatomic,strong)UILabel *NameLable;
//发生地质
@property(nonatomic,strong)UILabel *AddressLable1;
//地址2
@property(nonatomic,strong)UILabel *AddressLable2;
//签名
@property(nonatomic,strong)UILabel *textLable;
//黑线
@property(nonatomic,strong)UILabel *blackLable;
//数据源
@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,strong)UIView *Fview;
//故事收录地
@property(nonatomic,strong)UILabel *storyLable;
//相机标志
@property(nonatomic,strong)UIImageView *CimageView;
//收录于
@property(nonatomic,strong)UILabel *registLable;
//收录时间
@property(nonatomic,strong)UILabel *Rlable;
//详情
@property(nonatomic,strong)UIImageView *detailImageview;
//标题
@property(nonatomic,strong)UILabel *titleLable;
//点击查看详情
@property(nonatomic,strong)UILabel *ClickLable;
@end

@implementation DetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeView];
    [self parseData];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.rowHeight = 222;
}
-(void)makeView{
    _Dview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    _Fview = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 200)];
    _PicImageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 60, 60)];
    _PicImageView.layer.cornerRadius = 30;
    _PicImageView.layer.masksToBounds = YES;
    _NameLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 10, 100, 20)];
    _AddressLable1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 40, 50, 15)];
    _AddressLable1.text = @"故事发生在";
    _AddressLable1.font = [UIFont systemFontOfSize:10];
    _AddressLable2 = [[UILabel alloc]initWithFrame:CGRectMake(150, 40, 150, 15)];
    _textLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width, 130)];
    _blackLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 1)];
    _blackLable.backgroundColor = [UIColor blackColor];
    _storyLable = [[UILabel alloc]initWithFrame:CGRectMake(30, 20, self.view.frame.size.width - 40, 20)];
    _CimageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 50, 20, 20)];
    _CimageView.image = [UIImage imageNamed:@"86-camera.png"];
    _registLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 30, 15)];
    _registLable.text = @"收录于";
    _registLable.font = [UIFont systemFontOfSize:10];
    _Rlable = [[UILabel alloc]initWithFrame:CGRectMake(80, 50, 200, 15)];
    _Rlable.font = [UIFont systemFontOfSize:12];
    _detailImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 120)];
    _titleLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 30, self.view.frame.size.width - 60, 30)];
    _titleLable.font = [UIFont systemFontOfSize:25];
    _ClickLable = [[UILabel alloc]initWithFrame:CGRectMake(80, 80, self.view.frame.size.width - 160, 20)];
    _ClickLable.textAlignment = NSTextAlignmentCenter;
    [_detailImageview addSubview:_ClickLable];
    [_detailImageview addSubview:_titleLable];
    [_Fview addSubview:_detailImageview];
    [_Fview addSubview:_Rlable];
    [_Fview addSubview:_registLable];
    [_Fview addSubview:_CimageView];
    [_Fview addSubview:_storyLable];
    [_Dview addSubview:_blackLable];
    [_Dview addSubview:_textLable];
    [_Dview addSubview:_AddressLable2];
    [_Dview addSubview:_AddressLable1];
    [_Dview addSubview:_NameLable];
    [_Dview addSubview:_PicImageView];
    self.tableView.tableHeaderView = _Dview;
    self.tableView.tableFooterView = _Fview;
}
//懒加载
-(NSMutableArray *)allArray{
    if (_allArray == nil) {
        _allArray = [NSMutableArray array];
    }
    return _allArray;
}
-(void)parseData{
    [CQ_FanDataDownloadTools downloadDataWithURL:@"http://api.breadtrip.com/v2/new_trip/spot/?spot_id=2387666330" andMethod:@"GET" andBody:nil andBlock:^(NSData *data) {
        NSDictionary * allDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *obDic = allDic[@"data"];
        NSDictionary *dataDic = obDic[@"spot"];
//        NSDictionary *spotDic = dataDic[@"poi"];
        NSArray *poiDic = dataDic[@"detail_list"];
        for (NSDictionary *Dic in poiDic) {
            DetailModel *model = [[DetailModel alloc]init];
            [model setValuesForKeysWithDictionary:Dic];
            [self.allArray addObject:model];
        }
        [self.tableView reloadData];
        NSDictionary *tripDic = obDic[@"trip"];
        NSDictionary *userDic = tripDic[@"user"];
        [self.PicImageView sd_setImageWithURL:[NSURL URLWithString:userDic[@"avatar_l"]]];
        self.NameLable.text = [NSString stringWithString:userDic[@"name"]];
        self.textLable.text = [NSString stringWithString:dataDic[@"text"]];
        self.textLable.font = [UIFont systemFontOfSize:15];
        self.textLable.numberOfLines = 0;
        NSDictionary *poDic = dataDic[@"poi"];
        NSString *string = poDic[@"name"];
       NSMutableAttributedString *Str = [[NSMutableAttributedString alloc]initWithString:string];
        [Str addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(0, Str.length)];
        self.AddressLable2.attributedText = Str;
        self.AddressLable2.font = [UIFont systemFontOfSize:10];
        _storyLable.text = tripDic[@"name"];
        _Rlable.text = tripDic[@"date_added"];
        NSDictionary *tarDic = dataDic[@"target"];
        [_detailImageview sd_setImageWithURL:[NSURL URLWithString:tarDic[@"cover"]]];
        _titleLable.text = tarDic[@"title"];
        NSString *String = @"点击查看详情";
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:String];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, str.length)];
        _ClickLable.attributedText = str;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return self.allArray.count;
            break;
        case 1:
            
           
        default:
            return 0;
            break;
    }
}

/**/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[DetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    // Configure the cell...
    DetailModel *model = self.allArray[indexPath.row];
    cell.model = model;
    return cell;
}

-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 1;
}
-(CGFloat )tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
